package com.css.dzj.dzjportal.modules.promulgate.controller;

import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.modules.promulgate.entity.ApExecutorInfoEntity;
import com.css.dzj.dzjportal.modules.promulgate.service.ApExecutorInfoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ApExecutorInfo")
public class ApExecutorInfoController {
    @Autowired
    private ApExecutorInfoService apExecutorInfoService;

    /**
     * 查询所有执法人员
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/allList")
    public R allList(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                     @RequestParam(name = "pageNo" ,required = false)Integer pageNo){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<ApExecutorInfoEntity> tlist =  apExecutorInfoService.selectAll(pageRequest,pageSize,pageNo);
            return R.ok().put("data",tlist);
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询执法人员
     * @param pageSize
     * @param pageNo
     * @param executorName
     * @param executorCharacter
     * @param executorArea
     * @param executorCategory
     * @return
     */
    @GetMapping("/getSelectInfo")
    public R getSelectInfo(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                           @RequestParam(name = "pageNo" ,required = false)Integer pageNo,
                           @RequestParam(name = "executorName" ,required = false)  String executorName,
                           @RequestParam(name = "executorCharacter" ,required = false) String executorCharacter,
                           @RequestParam(name = "executorArea" ,required = false) String executorArea,
                           @RequestParam(name = "executorCategory" ,required = false)   String executorCategory){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<ApExecutorInfoEntity> tlist =  apExecutorInfoService.getSelectInfo(pageRequest,pageSize,pageNo,
                                                                                            executorName, executorCharacter,
                                                                                            executorArea,executorCategory);
            return R.ok().put("data",tlist);
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    /**
     * 查询详情
     */
    @GetMapping("/selectOne")
    public R allList(String id){
        try {
            ApExecutorInfoEntity pro =  apExecutorInfoService.getExecutorInfo(id);
            return R.ok().put("data",pro);
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    /**
     * 导出
     * 导出选中行数据，待定
     *
     * @param response
     * @param ids
     * @return
     */
    @GetMapping("/exportByIds")
    public R exportHolidayToExcel(HttpServletResponse response, @RequestParam String ids) {
        try {
            List<ApExecutorInfoEntity> executorEntities = apExecutorInfoService.getIds(ids);
            apExecutorInfoService.exportExecutorToExcel(response, executorEntities);
            //baseService.logSuccess(3, "执法人员导出成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //baseService.logFailed(3, "执法人员导出失败", "2");
            return R.error(e.getMessage());
        }
    }
    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam(name = "executorName" ,required = false)  String executorName,
                    @RequestParam(name = "executorCharacter" ,required = false) String executorCharacter,
                    @RequestParam(name = "executorArea" ,required = false) String executorArea,
                    @RequestParam(name = "executorCategory" ,required = false)   String executorCategory) {
        try {
            List<ApExecutorInfoEntity> executorEntities = apExecutorInfoService.getalls(executorName, executorCharacter,
                                                                                                executorArea,executorCategory);
            apExecutorInfoService.exportExecutorToExcel(response, executorEntities);
            //baseService.logSuccess(3, "执法人员导出成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //baseService.logFailed(3, "执法人员导出失败", "2");
            return R.error(e.getMessage());
        }
    }

}
